let studentList = [];//存储学生签到数据
let result = null;

$(function () {
    findAll();
});

/**
 * 模糊查询
 */
function findAll() {

    //得到当前用户ID，判断是否是组长
    let userCode = sessionStorage.getItem("userCode");
    let data = {
        userCode: userCode,
    };

    $.ajax({
        url: 'http://localhost:9000/sign/findTeamMemberByUserCode',
        data: data,
        type: 'get',
        dataType: 'json',
        headers: HEADERS,
        async: false,
        contentType: 'application/json;charset=utf-8',
        success: function (data) {
            setData(data.data);
            result = data.data;
        }
    });

}

/**
 * 数据展示
 */
function setData(data) {
    let html = '';
    for (let i = 0; i < data.length; i++) {

        html += ' <tr>\n' +
            '                <td>' + data[i].id + '</td>\n' +
            '                <td>' + data[i].code + '</td>\n' +
            '                <td>' + (data[i].username === undefined || data[i].username === 'null' ? '' : data[i].username) + '</td>\n' +
            '                <td>' + (data[i].avatar === undefined || data[i].avatar === 'null' ?
                '<img src="/upload/img_null.png" width="80px" height="30px">' :
                '<img src="' + data[i].avatar + '" width="80px" height="30px">') + '</td>\n' +
            '                <td>' + (data[i].nickname === undefined || data[i].nickname === 'null' ? '' : data[i].nickname) + '</td>\n' +
            '                <td>' + (data[i].phone === undefined || data[i].phone === 'null' ? '' : data[i].phone) + '</td>\n' +
            '                <td>' +
            '               <input type="radio" name="signStatus' + data[i].id + '" value="normal" title="正常" onclick="signLate(' + data[i].id + ')">正常' +
            '               <input type="radio" name="signStatus' + data[i].id + '" value="ask_for_leave" title="请假" onclick="signLate(' + data[i].id + ')">请假' +
            '               <input type="radio" name="signStatus' + data[i].id + '" value="late" title="迟到" onclick="signLate(' + data[i].id + ')">迟到' +
            '               <input type="radio" name="signStatus' + data[i].id + '" value="leave_early" title="早退" onclick="signLate(' + data[i].id + ')">早退' +
            '               <input type="radio" name="signStatus' + data[i].id + '" value="absenteeism" title="旷课" onclick="signLate(' + data[i].id + ')">旷课' +
            '               </td>\n' +
            '                <td><input type="datetime-local" id="lateTime' + data[i].id + '" class="hidden">' +
            '                   <input type="hidden" id="signTime' + data[i].id + '"> </td>\n' +
            '            </tr>';
    }
    $("#list").html(html);
}

/**
 * 签到状态
 * @param id
 */
function signLate(id) {
    //获取选中的值
    let signStatus = $("input[name='signStatus" + id + "']:checked").val();
    let lateTime = $('#lateTime' + id);
    $('#signTime' + id).val(getNowFormatDate());//签到时间
    if (signStatus === 'late') {
        lateTime.attr('class', "show");
    } else {
        lateTime.attr('class', "hidden");
    }
}

/**
 * 提交
 */
function commit() {
    let signStatus = '';
    let lateTime = '';
    let signTime = '';
    let signStatusList = [];
    let lateTimeList = [];
    let signTimeList = [];
    let userCodes = [];
    let data = {};
    //遍历取值
    for (let i = 0; i < result.length; i++) {
        signStatus = $("input[name='signStatus" + result[i].id + "']:checked").val();
        lateTime = $('#lateTime' + result[i].id).val();
        signTime = $('#signTime' + result[i].id).val();
        signStatusList.push(signStatus);
        lateTimeList.push(lateTime);
        signTimeList.push(signTime);
        userCodes.push(result[i].code);
    }
    //将json对象装进数组中
    for (let j = 0; j < result.length; j++) {
        data = {
            userCode: userCodes[j],
            signStatus: signStatusList[j],
            lateTime: lateTimeList[j],
            signTime: signTimeList[j]
        };
        studentList.push(data);
    }
    saveData();
}

/**
 * 保存签到数据
 */
function saveData() {
    $.ajax({
        url: 'http://localhost:9000/sign/saveSignData',
        data: JSON.stringify(studentList),
        type: 'post',
        dataType: 'json',
        headers: HEADERS,
        async: false,
        contentType: 'application/json;charset=utf-8',
        success: function (data) {
            if (data.code === 200) {
                alert("签到完成");
                window.location.href = 'signList.html';
            } else {
                alert("签到失败")
            }
        }
    });
}
